package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPlan;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.FarmAccountsVO;
import com.digitalchina.lxnsy.toolkit.DateStyle;
import com.digitalchina.lxnsy.toolkit.DateUtil;
import com.digitalchina.lxnsy.toolkit.JointImageUrl;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;

import java.io.Serializable;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/9/1 15:50
 * 修改备注：
 * ================================================
 */

public class FarmAccountDetailActivity extends BaseActivity {
    @Bind(R.id.iv_img)
    ImageView ivImg;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_money)
    TextView tvMoney;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.layout_time)
    RelativeLayout layoutTime;
    @Bind(R.id.nineGrid)
    NineGridView nineGrid;
    @Bind(R.id.et_remark)
    TextView etRemark;

    private FarmAccountsVO vo;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_farm_account_detail);
        setTitle("记账详情");
    }

    @Override
    public void initMember() {
        vo = (FarmAccountsVO) getIntent().getSerializableExtra("FarmAccountsVO");

        if (vo.getFarmAccountDescImageList() != null && vo.getFarmAccountDescImageList().size() > 0) {
            ArrayList<ImageInfo> imageInfo = new ArrayList<>();
            for (int i = 0; i < vo.getFarmAccountDescImageList().size(); i++) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(JointImageUrl.smallImageUrl(vo.getFarmAccountDescImageList().get(i).getImage()));
                info.setBigImageUrl(JointImageUrl.bigImageUrl(vo.getFarmAccountDescImageList().get(i).getImage()));
                imageInfo.add(info);
            }
            nineGrid.setAdapter(new NineGridViewClickAdapter(this, imageInfo));
        }

        if (vo.getAccountType() != null) {
            tvName.setText(vo.getAccountType().getName());
            for (int i = 0; i < Constant.FARM_ACCOUNT_TYPE_ARRAY.length; i++) {
                if (Constant.FARM_ACCOUNT_TYPE_ARRAY[i].equals(vo.getAccountType().getCode())) {
                    ivImg.setImageResource(Constant.FARM_ACCOUNT_IMAGE_ARRAY[i]);
                    vo.setImg(Constant.FARM_ACCOUNT_IMAGE_ARRAY[i]);
                    break;
                }
            }
        }

        if (vo.getType() == 0) {
            //收入
            tvMoney.setText("+" + vo.getMoney());
            tvMoney.setTextColor(getResources().getColor(R.color.app_color));
        } else {
            //支出
            tvMoney.setText("-" + vo.getMoney());
            tvMoney.setTextColor(getResources().getColor(R.color.colorFontRed));
        }


        tvTime.setText(DateUtil.StringToString(vo.getAccountDate(), DateStyle.YYYY_MM_DD));
        etRemark.setText(vo.getRemark() + "");
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {


        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MSG.MSG_DELETE_FARM_ACCOUNT_SUCCESS:
                        showToast("删除成功");
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case MSG.MSG_DELETE_FARM_ACCOUNT_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };

    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }

    @OnClick({R.id.tv_del, R.id.tv_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_del:
                new MaterialDialog.Builder(FarmAccountDetailActivity.this)
                        .title("提示")
                        .content("确定要删除该条记账信息？")
                        .positiveText("删除")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                BusinessPlan.deleteFarmAccount(FarmAccountDetailActivity.this, vo.getId(), mHandler);
                            }
                        })
                        .show();
                break;
            case R.id.tv_edit:
                Intent intent = new Intent(FarmAccountDetailActivity.this, AddFarmAccountActivity.class);
                intent.putExtra("FarmAccountsVO", (Serializable) vo);
                startActivityForResult(intent, 97);
                break;
        }
    }
}
